import debounce from 'lodash.debounce';
import type { Context } from 'chartjs-plugin-datalabels';
import { TooltipItem, ChartType } from 'chart.js';
const closeTooltipImpl = (context: TooltipItem<ChartType>) => {
	// canvas is null means that dom is unmounted
	if (!context.chart.canvas) return;
	if (context.chart.tooltip?.getActiveElements().length || 0 > 0) {
		context.chart.tooltip?.setActiveElements([], { x: 0, y: 0 });
		context.chart?.update();
	}
};

export const getCloseTooltip = (ms = 6000) => debounce(closeTooltipImpl, ms);

